
import 'package:flutter/material.dart';
import 'package:library_base/base/base_app_state_page.dart';
import 'package:library_base/ext/bool_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/get_util.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/button/ink_bg_well.dart';
import 'package:library_base/widget/dialog/dialog_util.dart';
import 'package:module_administrator/page/create_purchase_order/single_head_entry/single_head_entry_controller.dart';
import 'package:module_administrator/page/create_purchase_order/single_head_entry/single_head_entry_ui.dart';

class SingleHeadEntryPage extends StatefulWidget {
  const SingleHeadEntryPage({Key? key}) : super(key: key);

  @override
  _SingleHeadEntryPageState createState() => _SingleHeadEntryPageState();
}

class _SingleHeadEntryPageState extends BaseState<SingleHeadEntryPage, SingleHeadEntryController> {
  double weight = -1.0;

  double serviceCharge = -1.0;

  int startPosition = -1;

  int endPosition = -1;

  @override
  void onCreate() {
    setNavigationBarColor(ThemeUtil.getTheme().colorScheme.secondary);
    setTitle(MultiLanguage.singleHeadEntry.tr);
  }

  @override
  Widget getNavBarRightWidget() {
    return batchEditWell(weight, serviceCharge, onTap: (weight, serviceCharge) {
      DialogUtil.showSingleHeadDialog(arguments: {
        'barcodePrefix': SPUtil.barcodePrefix,
        'initialValue': SPUtil.getInt(SPUtil.key_initialValue, defValue: -1),
        'startPosition': startPosition,
        'endPosition': endPosition,
        'maxPosition': baseGetxController!.barcodeControllers.length,
        'weight': weight,
        'serviceCharge': serviceCharge,
        'CallBack': batch,
      });
    });
  }

  void batch(
    String barcodePrefix,
    int initialValue,
    int startPosition,
    int endPosition,
    double weight,
    double serviceCharge,
  ) {
    SPUtil.barcodePrefix = barcodePrefix;
    SPUtil.initialValue = initialValue;
    this.startPosition = startPosition;
    this.endPosition = endPosition;
    this.weight = weight;
    this.serviceCharge = serviceCharge;

    var temporaryStartPosition = startPosition == -1 ? 0 : startPosition;
    var temporaryEndPosition =
        endPosition == -1 || endPosition >= baseGetxController!.barcodeControllers.length
            ? (baseGetxController!.barcodeControllers.length - 1)
            : endPosition;

    int autoIncrement = 0;
    for (int i = temporaryStartPosition; i <= temporaryEndPosition; i++) {
      var barcodeController = baseGetxController!.barcodeControllers[i];
      var weightController = baseGetxController!.weightControllers[i];
      var serviceChargeController = baseGetxController!.serviceChargeControllers[i];
      barcodeController.text = "${SPUtil.barcodePrefix}${initialValue + autoIncrement}";
      autoIncrement++;
      if (weight != -1.0) {
        weightController.text = "$weight";
      }
      if (serviceCharge != -1.0) {
        serviceChargeController.text = "$serviceCharge";
      }
    }
    baseGetxController!.isCanSave.value = baseGetxController!.isCanClickDetermine();
    baseGetxController!.totalWeight.value = baseGetxController!.calculateTotalWeight();
    baseGetxController!.totalServiceCharge = baseGetxController!.calculateTotalServiceCharge();
    setSafeState();
  }

  @override
  SingleHeadEntryController registerController() => Get.find<SingleHeadEntryController>();

  @override
  Widget? getMainWidget(BuildContext context) {
    return Scaffold(
      body: WillPopScope(
        onWillPop: () async {
          GetHelper.back();
          return false;
        },
        child: Column(
          children: [
            buildHead(),
            Expanded(
              child: Material(
                child: MediaQuery.removePadding(
                  context: context,
                  removeTop: true,
                  removeBottom: true,
                  child: ListView(
                    children: _buildItem(),
                  ),
                ),
              ),
            ),
            Container(
                padding: EdgeInsets.only(top: 21.w, right: 24.w),
                height: 166.w,
                decoration: const BoxDecoration(color: Colors.white, boxShadow: [
                  BoxShadow(
                      offset: Offset(0.0, 5.0), //阴影xy轴偏移量
                      blurRadius: 15.0, //阴影模糊程度
                      spreadRadius: -11 //阴影扩散程度
                      )
                ]),
                alignment: Alignment.topRight,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(
                        child: Stack(children: [
                      Positioned(
                        left: 24.w,
                        top: 8.w,
                        child: GetBuilder<SingleHeadEntryController>(
                          builder: (logic) {
                            return Text(
                                '${MultiLanguage.totalWeight.tr}: ${logic.totalWeight.value}${SPUtil.isJin.unitStrZh()}');
                          },
                        ),
                      ),
                    ])),
                    GetBuilder<SingleHeadEntryController>(
                      builder: (logic) {
                        return GradientButton(
                          ThemeUtil.getTheme(),
                          width: 184.w,
                          height: 77.w,
                          text: MultiLanguage.save.tr,
                          onPressed: !baseGetxController!.isCanSave.value
                              ? null
                              : baseGetxController!.determine,
                        );
                      },
                    ),
                  ],
                )),
          ],
        ),
      ),
    );
  }

  List<Widget> _buildItem() {
    var isEdit = baseGetxController!.isEdit;
    var itemCount =
        isEdit ? baseGetxController!.goodsChildInfo.length : baseGetxController!.itemCount.value;
    List<Widget> widgets = [];
    for (int i = 0; i < itemCount; i++) {
      widgets.add(Padding(
        padding: EdgeInsets.only(top: 24.w),
        child: Row(
          children: [
            Expanded(
                flex: 1,
                child: Text(
                  '${i + 1}',
                  textAlign: TextAlign.center,
                  style: TextStyles.textNormal28,
                )),
            Expanded(
                flex: 6,
                child: Row(children: [
                  Text(baseGetxController!.prefix, style: TextStyles.textNormal28),
                  gapW8,
                  Expanded(
                    child: buildInput(
                        controller: baseGetxController!.barcodeControllers[i],
                        keyboardType: TextInputType.text,
                        onChanged: baseGetxController!.barcodeChange,
                      margin: EdgeInsets.only(right: 8.w),
                      // boxDecoration: BoxDecoration(
                      //     color: ThemeUtil.getTheme().colorScheme.secondary,
                      //     borderRadius: const BorderRadius.only(topRight: Radius.circular(30.0),bottomRight: Radius.circular(30.0)),
                      //     border: Border.all(color: ThemeUtil.getTheme().sixthTextColor(), width: 0.7)),
                    )
                  )
                ])),
            Expanded(
                flex: 3,
                child: buildInput(
                    controller: baseGetxController!.weightControllers[i],
                    onChanged: baseGetxController!.weightChange)),
            Expanded(
                flex: 3,
                child: buildInput(
                    controller: baseGetxController!.serviceChargeControllers[i],
                    onChanged: baseGetxController!.serviceChargeChange)),
          ],
        ),
      ));
    }
    return widgets;
  }
}
